package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsArticle;
import com.csun.dsms.model.domain.DsmsArticleResource;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.service.DsmsArticleResourceService;
import com.csun.dsms.service.DsmsArticleService;
import com.github.pagehelper.PageInfo;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class DsmsArticleController {
    @Resource
    private DsmsArticleService dsmsArticleServiceImpl;

    @Resource
    private DsmsArticleResourceService articleResourceService;

    /*
     * 公告添加
     * */
    @Authorization
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper articleAdd(@CurrentUser DsmsUser login, @RequestBody DsmsArticle dsmsArticle) {
        dsmsArticle.setCreator(login.getLoginName());
        dsmsArticle.setCreatorId(login.getId());
        dsmsArticle.setLastOperator(login.getLoginName());
        dsmsArticle.setLastOperatorId(login.getId());
        Integer save = dsmsArticleServiceImpl.save(dsmsArticle);

        Long id = dsmsArticle.getId();
        List<DsmsArticleResource> articleResourceList = dsmsArticle.getArticleResourceList();
        if (!CollectionUtils.isEmpty(articleResourceList)) {
            for (DsmsArticleResource fileRelevance : articleResourceList) {
                fileRelevance.setArticleId(id);
                fileRelevance.setFileName(this.getFileName(fileRelevance.getResourcePath()));
                articleResourceService.save(fileRelevance);
            }
        }


        if (save < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 公告删除
     * */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper articleDelete(@RequestBody DsmsArticle dsmsArticle) {
        Integer i = dsmsArticleServiceImpl.deleteByKey(dsmsArticle);
        if (i < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 公告修改
     * */
    @Authorization
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper articleUpdate(@CurrentUser DsmsUser login, @RequestBody DsmsArticle dsmsArticle) {
        dsmsArticle.setLastOperator(login.getLoginName());
        dsmsArticle.setLastOperatorId(login.getId());
        dsmsArticle.setUpdateTime(new Date());
        Integer i = dsmsArticleServiceImpl.update(dsmsArticle);

//        Example example = new Example(DsmsArticleResource.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("articleId", );
        articleResourceService.deleteByArticleId(dsmsArticle.getId());
        List<DsmsArticleResource> articleResourceList = dsmsArticle.getArticleResourceList();
        if (!CollectionUtils.isEmpty(articleResourceList)) {
            for (DsmsArticleResource articleResource : articleResourceList) {
                articleResource.setArticleId(dsmsArticle.getId());
                articleResource.setFileName(this.getFileName(articleResource.getResourcePath()));
                articleResourceService.save(articleResource);
            }
        }


        if (i < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 公告查询
     * */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public Wrapper articleSelect(@RequestBody DsmsArticle dsmsArticle) {
        if (dsmsArticle.getId() != null) {
            DsmsArticle art = new DsmsArticle();
            art.setId(dsmsArticle.getId());
            DsmsArticle article = dsmsArticleServiceImpl.selectByKey(dsmsArticle);
            art.setViews(article.getViews() + 1);
            dsmsArticleServiceImpl.update(art);
        }
        List<DsmsArticle> dsmsArticles = dsmsArticleServiceImpl.query(dsmsArticle);
        if (!CollectionUtils.isEmpty(dsmsArticles)) {
            for (DsmsArticle article : dsmsArticles) {
                Example example = new Example(DsmsArticleResource.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("articleId", article.getId());
                List<DsmsArticleResource> dsmsArticleResources = articleResourceService.selectByExample(example);
                article.setArticleResourceList(dsmsArticleResources);
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, new PageInfo<>(dsmsArticles));
    }


    private String getFileName(String path) {
        return path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
    }
}
